﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineFamilyCalendar.Model
{
    public class LoginInformation : IValidate
    {
        private bool _isAuthenticated;
        public Guid LoginId { get; internal set; }
        public string LoginName { get; internal set; }
        public string Password { get; internal set; }
        public bool IsLocked { get; internal set; }

        internal LoginInformation()
        {
        }

        public static LoginInformation Create(string loginName, string password)
        {
            LoginInformation loginInformation = new LoginInformation
            {
                LoginId = Guid.NewGuid(),
                LoginName = loginName,
                Password = password,
                IsLocked = true
            };

            return loginInformation;
        }

        void IValidate.Validate(ChangeAction action)
        {
            if (action == ChangeAction.Insert || action == ChangeAction.Update)
            {
                if (LoginId == Guid.Empty)
                    throw new InvalidOperationException("LoginId can not be empty");

                if (String.IsNullOrEmpty(this.LoginName))
                    throw new InvalidOperationException("LoginName can not be empty");

                if (String.IsNullOrEmpty(this.Password))
                    throw new InvalidOperationException("Password can not be empty");
            }            
        }

        public bool IsAuthenticated
        {
            get
            {
                return _isAuthenticated;
            }
        }

        public void Authenticate(string password)
        {
            if (Password == password)
                _isAuthenticated = true;
            else
                _isAuthenticated = false;
        }

        public void Activate()
        {
            IsLocked = false;
        }

        public void ChangePassword(string newPassword)
        {
            Password = newPassword;
        }
    }
}
